/*
	Comment: is there any thing that is TCHAR * ??

	Compile for both Unicode and ANSI, but still problem when open these two formats!
	Unicode and ANSI code are tested to work in Unicode and ANSI compiling

	However, the Big Ended Unicode could not be handled properly now 
*/


#include "utility.h"

void Utility::DoFileOpen()
{
	ofn.lStructSize = sizeof(ofn);
	ofn.hwndOwner = hwnd;
	ofn.lpstrFilter = _T("Text File (*.txt)\0*.txt\0All Files (*.*)\0*.*\0");
	ofn.lpstrFile = szFileName;
	ofn.nMaxFile = MAX_PATH;
	ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;	// attribute to be changed in the future
	ofn.lpstrDefExt = _T("txt");

	if(GetOpenFileName(&ofn))
	{
		if(LoadTextFileToEdit(szFileName))
		{
			SetWindowText(hwnd, szFileName);
		}
	}
}

BOOL Utility::LoadTextFileToEdit(TCHAR * pszFileName)
{
	HANDLE hFile;
	BOOL bSuccess = FALSE;

	hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
	if(hFile != INVALID_HANDLE_VALUE)
	{
		DWORD dwFileSize = GetFileSize(hFile, NULL);
		if(dwFileSize != 0xFFFFFFFF)
		{			
			TCHAR * pFileText;
			// Use heap allocate api
			HANDLE hHeap = GetProcessHeap();
			if(hHeap == NULL)
			{
				MessageBox(NULL, _T("Get Process Heap Handler Fail!"), _T("ERROR"), MB_ICONEXCLAMATION | MB_OK);
			}
			LPSTR tBuffer = (LPSTR)HeapAlloc(hHeap, HEAP_ZERO_MEMORY, dwFileSize + 2);	// for the possible of unicode, add 2
			if(tBuffer != NULL) {									// and use char here because of readfile use byte!
				DWORD dwRead;
				if(ReadFile(hFile, tBuffer, dwFileSize, &dwRead, NULL))
				{
					tBuffer[dwFileSize] = tBuffer[dwFileSize + 1] = '\0';						// Add null terminator
					pFileText = (TCHAR *)tBuffer;						// Convert to the output format

					if( (tBuffer[0] == -1 && tBuffer[1] == -2) ||		// if the txt is in Unicode
						(tBuffer[0] == -2 && tBuffer[1] == -1) )
					{	
						if( sizeof(TCHAR) == sizeof(CHAR) )			// if the compiler is in ANSI
						{
							pFileText = (TCHAR *)HeapAlloc(hHeap, HEAP_ZERO_MEMORY, dwFileSize + 1);
							WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, (LPCWSTR)tBuffer + 1, // delete the flag of the txt
								-1, (LPSTR)pFileText, dwFileSize + 1, NULL, NULL);
						}		
						if(SetWindowText(GetDlgItem(hwnd, IDC_MAIN_EDIT), pFileText))
						{
							bSuccess = TRUE;	// things run smoothly
						}			
					}
					else												// if the txt is in ANSI
					{
						if( sizeof(TCHAR) / sizeof(CHAR) == 2 )			// if the compiler is in Unicode
						{
							pFileText = (TCHAR *)HeapAlloc(hHeap, HEAP_ZERO_MEMORY, dwFileSize * 2 + 4);
							MultiByteToWideChar(CP_ACP, MB_COMPOSITE, tBuffer, -1, (LPWSTR)pFileText, dwFileSize + 2);
						}						
						if(SetWindowText(GetDlgItem(hwnd, IDC_MAIN_EDIT), pFileText))
						{
							bSuccess = TRUE;	// things run smoothly
						}			
					}
				}

				if(pFileText != (TCHAR *)tBuffer)
				{
					HeapFree(hHeap, NULL, pFileText);	// only possible to free memory when successfully allocated
				}
				HeapFree(hHeap, NULL, tBuffer);	// only possible to free memory when successfully allocated
			}
		}
		CloseHandle(hFile);
	}
	return bSuccess;
}

void Utility::DoFileSave()
{

}